package com.ruoyi.web.rmNew.service.http;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.web.core.config.ConfigLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AqHttpService2 {
    @Autowired
    private ConfigLoader configLoader;

    private JSONObject getAqHeaders() {
        List<String> cookie = FileUtil.readLines(configLoader.getGamenamesPath() + "aq-cookie.txt", "utf8");
        JSONObject header = new JSONObject();
        header.put("authorization", cookie.get(0));
        return header;
    }

    public JSONObject getAqList(int page, String filterType) {
        String url = StrUtil.format("{}api/app/match/market/matches?filterType={}&page={}&size=20",
                ConfigLoader.getConfig().getAqHost(), filterType,page);
        return HttpUtils.httpJsonOfZyt(url, "get", null, getAqHeaders());
    }

    public void getAqDetail(String gameId, String path) {
        String url = StrUtil.format("{}api/app/bet/betting/correct-score/%7BfixtureId%7D",
                ConfigLoader.getConfig().getAqHost());
        JSONObject body = new JSONObject();
        body.fluentPut("id", gameId);
        JSONObject res = HttpUtils.httpJsonOfZyt(url, "post", body, getAqHeaders());
        FileUtil.writeString(res.toJSONString(), path, "utf8");
    }
}
